package com.aaa.controller;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.entity.*;
import com.aaa.service.*;
import com.aaa.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/7/6 9:18
 * @description：测试人员控制器
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("front")
public class FrontController   extends BaseController{
    @Autowired
    private ViewTestPlanService viewTestPlanService;
    @Autowired
    private QuestionFpaService questionFpaService;
    @Autowired
    private QuestionSasService questionSasService;
    @Autowired
    private TblQuestionSdsService tblQuestionSdsService;
    @Autowired
    private TesterFpaService testerFpaService;

    @Autowired
    private TestResultFpaService testResultFpaService;
    @Autowired
    private TestResultSasService testResultSasService;
    @Autowired
    private TestResultSdsService testResultSdsService;

    MyReturn myReturn= new MyReturn();
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 跳转到用户测试页面
     * @create time: 2022/7/6 11:15
     * @param modelAndView
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping("toTest.do")
    public ModelAndView toTest(ModelAndView modelAndView){
        modelAndView.setViewName("front/testLogin");
        return modelAndView;
    }


    @RequestMapping("checkTest.do")
    @ResponseBody
    public MyReturn checkTest(@RequestBody TesterVo tester, HttpSession httpSession){
        //根据测试邀请码和手机号判断学生是否已经做过测试
        ViewTestPlan testPlan = viewTestPlanService.queryByTestCode(tester.getTestCode());
        if(ObjectUtil.isNotEmpty(testPlan)){
            Date date=new Date();
            //查询时间
            if (date.after(testPlan.getTestBegin())&& date.before(testPlan.getTestEnd())) {
                //设置重复flag,默认不重复
                boolean flag = false;
                switch (testPlan.getType()) {
                    case Constants.FPA:
                        //根据手机号查询是否之前做过测试
                        List<Map<String, Object>> byPhone = testerFpaService.findTesterByPhone(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone) && byPhone.size() > 0) {
                            flag = true;
                        }
                        break;
                    case Constants.SAS:
                        //根据手机号查询是否之前做过测试
                        List<Map<String, Object>> byPhone1 = testerFpaService.findSasTesterByPhone(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone1) && byPhone1.size() > 0) {
                            flag = true;
                        }
                        break;
                    case Constants.SDS:
                        //根据手机号查询是否之前做过测试
                        List<Map<String, Object>> byPhone2 = testerFpaService.findSdsTesterByPhone(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone2) && byPhone2.size() > 0) {
                            flag = true;
                        }
                        break;
                    default:
                        break;
                }
                if(flag){
                    myReturn.setCode(1);
                    myReturn.setMsg("对不起，您的手机号已经做过测试了！");
                }else {
                    myReturn.setCode(0);
                    //将测试计划id保存在实体对象tester中
                    tester.setTestPlanId(testPlan.getId());
                    //将测试类型保存在实体对象tester中
                    tester.setType(testPlan.getType());
                    //保存测试计划名称
                    tester.setTestName(testPlan.getTestName());
                    //将测试者信息保存在session中
                    httpSession.setAttribute("tester",tester);
                    //  myReturn.setData(tester);
                }
            }else {
                myReturn.setCode(1);
                myReturn.setMsg("对不起，测试码已失效");
            }
        }else {
            myReturn.setCode(1);
            myReturn.setMsg("对不起，邀请码无效，请确认！");
        }

        return myReturn;

    }
    @RequestMapping("findQuestion.do")
    public ModelAndView findQuestion(HttpSession httpSession,ModelAndView modelAndView){
        //获取session中保存的测试者信息
        TesterVo tester = (TesterVo) httpSession.getAttribute("tester");
        switch (tester.getType()){
            case Constants.FPA:
                //查询所有可以测试的fpa测试题
                List<QuestionFpa> questionFpaList = questionFpaService.findAllQuestion();
                modelAndView.addObject("questionList",questionFpaList);
                break;
            case Constants.SAS:
                //查询所有可以测试的sas测试题
                List<QuestionSas> questionSasList = questionSasService.findAllQuestion();
                modelAndView.addObject("questionList",questionSasList);
                break;
            case Constants.SDS:
                //查询所有可以测试的sds测试题
                List<TblQuestionSds> questionSdsList = tblQuestionSdsService.findAllQuestion();
                modelAndView.addObject("questionList",questionSdsList);
                break;
            default:break;

        }
        modelAndView.setViewName("front/testQuestion");
        return modelAndView;
    }

    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 批量保存测试结果
     * @create time: 2022/7/7 11:11
     * @param answerList
     * @return  MyReturn
     */
    @RequestMapping("saveBatchTestResult.do")
    @ResponseBody
    public MyReturn saveBatchTestResult(@RequestBody List<Map<String,Object>> answerList,HttpSession httpSession){

        //获取session中保存的测试者信息
        TesterVo tester = (TesterVo) httpSession.getAttribute("tester");

        if (tester.getType().equals(Constants.FPA)) {
            String testResult = testResultFpaService.saveTestResult(answerList, tester);
            myReturn.setData(testResult);
            return myReturn;
        }else if (tester.getType().equals(Constants.SAS)){
            String testResult = testResultSasService.saveTestResult(answerList, tester);
            myReturn.setData(testResult);
            return myReturn;
        } else if (tester.getType().equals(Constants.SDS)){
            String testResult = testResultSdsService.saveTestResult(answerList, tester);
            myReturn.setData(testResult);
            return myReturn;
        }
        return null;
    }
}
